//
//  MCCameraDefault.m
//  CreditGroup
//
//  Created by ltl on 15/9/11.
//  Copyright (c) 2015年 JPlay. All rights reserved.
//

#import "MCCameraDefault.h"
#import "MCCameraEventProcess.h"

@interface MCCameraDefault ()<UIActionSheetDelegate>
{
    UIViewController *rootViewController;
}
@property (strong, nonatomic) UIImageView *backImageView;
@property (strong, nonatomic) MCCameraEventProcess *eventClass;
@property (assign, nonatomic) EActionSheetEventType actionSheetType;
@end

@implementation MCCameraDefault

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        NSLog(@"MCCameraDefault init is success");

        [self installRegister];
        
    }
    return self;
}
- (void)setIsHaveFrame:(EMCCameraIsHaveFrame)isHaveFrame{
    _isHaveFrame = isHaveFrame;
}
- (void)setIsClip:(EMCCameraIsClip)isClip{
    _isClip = isClip;
    self.eventClass.isClip = _isClip;

}
- (void)setRotation:(EMCImageRotationType)rotation{
    _rotation = rotation;
}

- (void)setSize:(CGSize)size{
    _size = size;
}

- (void)setFormat:(EMCImageFormat)format{
    _format = format;
}

- (void)setQuality:(CGFloat)quality{
    _quality = quality;
}

- (void)installRegister
{
    __weak MCCameraDefault *weakself = self;
    self.eventClass = [[MCCameraEventProcess alloc] init];
    self.eventClass.isClip = _isClip;
    self.eventClass.rotation = _rotation;
    self.eventClass.size     = _size;
    self.eventClass.format   = _format;
    self.eventClass.quality  = _quality;
    self.eventClass.type     = _type;
    self.eventClass.returnImageBlock= ^(UIImage *image)
    {
      // 拿到相机/相册返回的图片
        weakself.cameraDefaultReturnImageBlock(image);
    };
    self.eventClass.returnImageDataBlock = ^(NSData *imageData){
        weakself.cameraDefaultReturnImageDataBlock(imageData);
    };
    [self showCameraSource];
}

- (void)showCameraSource
{
    NSInteger version = [[[[UIDevice currentDevice] systemVersion] substringToIndex:1] floatValue];
    if (version<8) {
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                       delegate:self
                                              cancelButtonTitle:@"取消"
                                         destructiveButtonTitle:nil
                                              otherButtonTitles:@"相机",@"相册", nil];
        
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        rootViewController = window.rootViewController;
        [actionSheet showInView:window];
        return;
    }
    
    // iOS 9
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil
                                                                             message:nil
                                                                      preferredStyle:UIAlertControllerStyleActionSheet];
    for (int i = 0; i<3; i++) {
        NSArray *titleArr = @[@"相机",
                              @"相册",
                              @"取消"];
        UIAlertAction *action = [UIAlertAction actionWithTitle:titleArr[i]
                                                         style:(i == 2 ? UIAlertActionStyleCancel : UIAlertActionStyleDefault)
                                                       handler:^(UIAlertAction * _Nonnull action) {
                                                           [self alertController:alertController clickedButtonAtIndex:i];
                                                       }];
        
        [alertController addAction:action];
    }
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    rootViewController = window.rootViewController;
    [rootViewController presentViewController:alertController animated:YES completion:^{
        NSLog(@"UIAlertController 弹出成功~");
    }];
}
#pragma mark - UIAlertControllerDelegate
- (void)alertController:(UIAlertController *)alertController clickedButtonAtIndex:(EActionSheetEventType)actionStype
{
    EActionSheetEventType type = (EActionSheetEventType)actionStype;
    // 记录点击事件
    if (self.eventBlock)
    {
        self.eventBlock(type);
    }
    [self.eventClass transmitClickEventsWithActionSheetType:type isHaveFrame:_isHaveFrame];
}

#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    EActionSheetEventType type = (EActionSheetEventType)buttonIndex;
    // 记录点击事件
    if (self.eventBlock)
    {
        self.eventBlock(type);
    }
    [self.eventClass transmitClickEventsWithActionSheetType:type isHaveFrame:_isHaveFrame];
}

- (void)dealloc{
    NSLog(@"MCCameraDefault = nil");
}

@end
